package cn.homework.web.servlet;

import cn.homework.domain.MainType;
import cn.homework.service.TypeService;
import cn.homework.service.impl.TypeServiceImpl;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * @author ACT
 */
@WebServlet("/typeServlet")
public class TypeServlet extends HttpServlet {
    private TypeService service = new TypeServiceImpl();

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String demand = request.getParameter("demand");
        switch (demand) {
            case "show": {
                productTypeShow(request, response);
                break;
            }
            case "add": {
                productTypeAdd(request, response);
                break;
            }
            case "delete": {
                productTypeDel(request, response);
                break;
            }
            case "query": {
                productTypeQuery(request, response);
                break;
            }
            default:
                break;
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    /**
     * 商品类别显示
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void productTypeShow(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<MainType> typeAllList = service.findAllProductType();
        HttpSession session = request.getSession();
        session.setAttribute("typeAllList", typeAllList);
        response.sendRedirect(request.getContextPath() + "/BackstageTypeShow.jsp");
    }

    /**
     * 商品类别添加
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void productTypeAdd(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> map = request.getParameterMap();
        MainType type = new MainType();
        try {
            BeanUtils.populate(type, map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        service.addProductType(type);
        productTypeShow(request, response);
    }

    /**
     * 商品类别删除
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void productTypeDel(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String type = request.getParameter("type");
        service.delProductTypeByType(type);
        productTypeShow(request, response);
    }

    /**
     * 按条件查询商品类别
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void productTypeQuery(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Map<String, String[]> map = request.getParameterMap();
        List<MainType> typeList = service.findProductTypeByCondition(map);
        HttpSession session = request.getSession();
        session.setAttribute("typeList", typeList);
        response.sendRedirect(request.getContextPath() + "/BackstageTypeQuery.jsp");
    }
}
